package cn.openea.perpartner.system.service.impl;

import cn.openea.perpartner.common.core.constant.UserConstants;
import cn.openea.perpartner.system.entity.SysUserUnion;
import cn.openea.perpartner.system.enums.AppletsEnums;
import cn.openea.perpartner.system.mapper.SysUserUnionMapper;
import cn.openea.perpartner.system.service.ISysUserUnionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-07-12 10:33
 */
@Service
public class SysUserUnionServiceImpl extends ServiceImpl<SysUserUnionMapper,SysUserUnion> implements ISysUserUnionService {


    @Override
    public String checkUserUniqueByOpenId(String openid) {
        LambdaQueryWrapper<SysUserUnion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserUnion::getOpenid,openid);
        return count(wrapper) >0? UserConstants.NOT_UNIQUE: UserConstants.UNIQUE;
    }

    @Override
    public String checkUserUnique(String openid, String unionid) {
        LambdaQueryWrapper<SysUserUnion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserUnion::getUnionid,unionid);
        wrapper.eq(SysUserUnion::getOpenid,openid);
        Long count = baseMapper.selectCount(wrapper);
        return count >0? UserConstants.NOT_UNIQUE: UserConstants.UNIQUE;
    }

    @Override
    public SysUserUnion selectByUserId(String userId, String unionid,String appid) {
        LambdaQueryWrapper<SysUserUnion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserUnion::getUnionid,unionid);
        wrapper.eq(SysUserUnion::getUserid,userId);
        wrapper.eq(SysUserUnion::getAppid, AppletsEnums.getApplet(appid).getAppId());
        return getOne(wrapper,false);
    }

    @Override
    public SysUserUnion selectByOpenId(String openId, String unionid) {
        LambdaQueryWrapper<SysUserUnion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserUnion::getUnionid,unionid);
        wrapper.eq(SysUserUnion::getOpenid,openId);
        return getOne(wrapper);
    }

    @Override
    public SysUserUnion selectByOpenId(String openid) {
        LambdaQueryWrapper<SysUserUnion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserUnion::getOpenid,openid);
        return getOne(wrapper);
    }
}
